package club.pchome.util;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @description(描述): 分页工具类
 */
@ApiModel(value = "分页工具类", description = "用于列表分页")
public class PageUtil<T> implements Serializable {
	private static final long serialVersionUID = 2309960061732381089L;
	@ApiModelProperty("当前页")
	private int currPage = 1; // 当前页
	@ApiModelProperty("总页数")
	private int totalPage; // 总页数
	@ApiModelProperty("上一页")
	private int prePage; // 上一页
	@ApiModelProperty("下一页")
	private int nextPage; // 下一页
	@ApiModelProperty("总记录数")
	private int totalNum; // 总记录数
	@ApiModelProperty("每页条数")
	private int pageSize = 10; // 每页条数
	@ApiModelProperty("获取记录的起始位子")
	private int startRow; // 获取记录的起始位子
	@ApiModelProperty("获取记录的结束位子")
	private int endRow; // 获取记录的结束位子
	@ApiModelProperty("前几页页码数量")
	private int prePagesNum = 5; // 前几页页码数量
	@ApiModelProperty("前几页页码集合")
	private List<Integer> prePages = null;// 前几页页码集合
	@ApiModelProperty("后几页页码数量")
	private int nextPagesNum = 5; // 后几页页码数量
	@ApiModelProperty("后几页页码集合")
	private List<Integer> nextPages = null;// 后几页页码集合
	@ApiModelProperty("每页数据")
	private List<T> dataList;

	public PageUtil(Integer pageSize) {
		if (null != pageSize) {
			this.pageSize = pageSize;
		}
	}

	public PageUtil(Integer currPage, Integer pageSize) {
		this.currPage = null != currPage ? currPage : 1;
		if (null != pageSize) {
			this.pageSize = pageSize;
		}
	}

	public PageUtil(int currPage, int pageSize, int totalNum) {
		this.currPage = currPage;
		this.pageSize = pageSize;
		this.pageUtilHandle(totalNum);
	}

	public PageUtil(String currPage, String pageSize) {
		this.setCurrPage(currPage);
		this.setPageSize(pageSize);
	}

	public PageUtil(String currPage, String pageSize, int totalNum) {
		this.setCurrPage(currPage);
		this.setPageSize(pageSize);
		this.pageUtilHandle(totalNum);
	}

	/**
	 * 设置翻页类的最大页数，并自动计算上一页，下一页，最大页等参数
	 * 
	 * @author lsz
	 * @param totalNum 最大条数
	 */
	public void pageUtilHandle(long totalNum) {
		this.pageUtilHandle(new Long(totalNum).intValue());
	}

	/**
	 * 核心计算方法，必须参数，当前页，每页条数，总记录数<br>
	 * 设置翻页类的最大页数，并自动计算上一页，下一页，最大页等参数<br>
	 * 初始化翻页类<br>
	 * 
	 * @author lsz
	 * @param totalNum 最大条数
	 */
	public void pageUtilHandle(int totalNum) {
		this.totalNum = totalNum;
		this.setTotalPage(this.totalNum, this.pageSize);
		this.setCurrPage(this.currPage, this.totalPage);
		this.setPrePage(this.currPage, this.totalPage);
		this.setNextPage(this.currPage, this.totalPage);
		this.setStartRow(this.currPage, this.pageSize);
		this.setEndRow(this.currPage, this.pageSize);
		this.setPrePages();
		this.setNextPages();
	}

	/**
	 * 设置当前页， 如果当前页大于最大页数，将当前页设为最大页数
	 * 
	 * @author lsz
	 * @param currPage
	 * @param totalPage
	 */
	public void setCurrPage(int currPage, int totalPage) {
		this.currPage = totalPage <= 0 ? 1 : currPage > totalPage ? totalPage : currPage < 0 ? 1 : currPage;
	}

	/**
	 * 设置最大页数
	 * 
	 * @author lsz
	 * @param totalNum
	 * @param pageSize
	 */
	public void setTotalPage(int totalNum, int pageSize) {
		if (0 == pageSize) {
			this.totalPage = 1;
		} else {
			this.totalPage = totalNum % pageSize == 0 ? totalNum / pageSize : totalNum / pageSize + 1;
		}
	}

	/**
	 * 设置上一页的页数
	 * 
	 * @author lsz
	 * @param currPage
	 * @param totalPage
	 */
	public void setPrePage(int currPage, int totalPage) {
		this.prePage = currPage <= 1 ? 1 : currPage >= this.totalPage ? this.totalPage - 1 : currPage - 1;
	}

	/**
	 * 设置下一页的页数
	 * 
	 * @author lsz
	 * @param currPage
	 * @param totalPage
	 */
	public void setNextPage(int currPage, int totalPage) {
		this.nextPage = currPage >= totalPage ? totalPage : currPage <= 0 ? totalPage > 1 ? 2 : 1 : currPage + 1;
	}

	/**
	 * 设置获取记录的起始位子
	 * 
	 * @author lsz
	 * @param currPage
	 * @param pageSize
	 */
	public void setStartRow(int currPage, int pageSize) {
		this.startRow = (currPage - 1) * pageSize;
	}

	/**
	 * 设置获取记录的结束位子
	 * 
	 * @author lsz
	 * @param currPage
	 * @param pageSize
	 */
	public void setEndRow(int currPage, int pageSize) {
		this.endRow = currPage * pageSize >= this.totalNum ? this.totalNum : currPage * pageSize;
	}

	/**
	 * 设置当前页
	 * 
	 * @author lsz
	 * @param currPage
	 */
	public void setCurrPage(String currPage) {
		if (null != currPage && currPage.matches("[0-9]*")) {
			this.currPage = Integer.valueOf(currPage) <= 0 ? 1 : Integer.valueOf(currPage);
		}
	}

	/**
	 * 设置每页条数
	 * 
	 * @author lsz
	 * @param pageSize
	 */
	public void setPageSize(String pageSize) {
		if (pageSize != null && pageSize.matches("[0-9]*")) {
			int pagesize = Integer.valueOf(pageSize);
			if (pagesize >= 0) {
				this.pageSize = pagesize;
			}
		}
	}

	/**
	 * 计算前n页页码
	 */
	public void setPrePages() {
		prePages = new ArrayList<Integer>();
		for (int i = prePagesNum; i > 0; i--) {
			int num = currPage - i;
			if (num <= 0) {
				continue;
			}
			prePages.add(num);
		}
	}

	/**
	 * 计算后n页页码
	 */
	public void setNextPages() {
		nextPages = new ArrayList<Integer>();
		for (int i = 1; i <= nextPagesNum; i++) {
			int num = currPage + i;
			if (num > totalPage) {
				break;
			}
			nextPages.add(num);
		}
	}

	public int getCurrPage() {
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getPrePage() {
		return prePage;
	}

	public void setPrePage(int prePage) {
		this.prePage = prePage;
	}

	public int getNextPage() {
		return nextPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	public int getTotalNum() {
		return totalNum;
	}

	public void setTotalNum(int totalNum) {
		this.totalNum = totalNum;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getStartRow() {
		return startRow;
	}

	public void setStartRow(int startRow) {
		this.startRow = startRow;
		this.currPage = 0 == this.pageSize ? 1 : (startRow / this.pageSize) + 1;
	}

	public int getEndRow() {
		return endRow;
	}

	public void setEndRow(int endRow) {
		this.endRow = endRow;
	}

	public int getPrePagesNum() {
		return prePagesNum;
	}

	public void setPrePagesNum(int prePagesNum) {
		this.prePagesNum = prePagesNum;
	}

	public List<Integer> getPrePages() {
		return prePages;
	}

	public void setPrePages(List<Integer> prePages) {
		this.prePages = prePages;
	}

	public int getNextPagesNum() {
		return nextPagesNum;
	}

	public void setNextPagesNum(int nextPagesNum) {
		this.nextPagesNum = nextPagesNum;
	}

	public List<Integer> getNextPages() {
		return nextPages;
	}

	public void setNextPages(List<Integer> nextPages) {
		this.nextPages = nextPages;
	}

	public List<T> getDataList() {
		return dataList;
	}

	public void setDataList(List<T> dataList) {
		this.dataList = dataList;
	}
}